<?php
include_once './base/HotPhpClientModel.php';
class servicenoteClientClass extends HotPhpClientModel
{

    /**
     * 执行文件名
     *
     * @property string
     */
    public $execFile = '';


    /**
     * 命令行提示信息
     *
     * @property string
     */
    protected $_usage = "\n\tcyclereportd client warring:\nUsage: [FILE] -f CONFIG_FILE [-q] \n";

    /**
     * 服务初始化
     *
     * @return void
     */
    public function init()
    {
    }

    /**
     * 客户端接口
     *
     * @param string $command
     *            输入的命令
     * @return void
     */
    public function client($command)
    {
        // 截取字符串
        $arrayComand = explode(' ', $command);

        if (is_array($arrayComand) == false || empty($arrayComand) == true || $arrayComand[0] == '') {
            return;
        }

        // 命令选择
        switch ($arrayComand[0]) {
            case 'help':
                // 帮助
                $this->cmdHelp();
                break;
            case 's':
                // 查看服务状态
                $this->cmdStats();
                break;
            case 'stats':
                // 查看服务状态
                $this->cmdStats();
                break;
            case 'logs':
                // 查看日志
                if (isset($arrayComand[1]) == false) {
                    $arrayComand[1] = 10;
                }
                if (isset($arrayComand[2]) == false) {
                    $arrayComand[2] = '';
                }
                $this->cmdLogs($arrayComand[1], $arrayComand[2]);
                break;
            case 'exit':
                // 退出
                exit();
                break;
            default:
                echo "Unknow Command, Please check the 'help' command! default is logs\n";
                $this->cmdLogs(10,'');
                break;
        }
    }

    /**
     * 帮助
     *
     * @return void
     */
    protected function cmdHelp()
    {
        echo "stats/s: Display the current status.\n";
        echo "logs [num] [file]:  Show log.\n";
        echo "exit: Exit the client.\n";
    }

    /**
     * 显示状态
     *
     * @return void
     */
    protected function cmdStats()
    {

        /*  echo '  TotalSyncTims : ' . $this->getStatusValue('T_SyncTims') . "\n";
          echo '  SessionLogs : ' . $this->getStatusValue('SessionLogs') . "\n";
          echo '  TotalErrorLogCount : ' . $this->getStatusValue('T_Err_Count') . "\n";
          echo '  Service ExceptionCount : ' . $this->getStatusValue('S_Ex_Count') . "\n";
          echo '  TimingService ExceptionCount : ' . $this->getStatusValue('T_Ex_Count') . "\n";

          echo '  SessionLogs1 : ' . $this->getStatusValue('SessionLogs1') . "\n";

          */
//        echo '  TotalSyncTims : ' . $this->getStatusValue('T_SyncTims') . "\n";
//        echo '  SessionLogs : ' . $this->getStatusValue('SessionLogs') . "\n";
//        //echo '  TotalWriteData : ' . $this->getStatusValue('T_WriteData') . "\n";
//        //echo '  LastWriteData : ' . $this->getStatusValue('L_WriteData') . "\n";
//        echo '  LastStartTime : ' . $this->getStatusValue('L_StartTime') . "\n";
//        echo '  LasEndTime : ' . $this->getStatusValue('L_EndTime') . "\n";
//        echo '  LastWriteSpeed : ' . $this->getStatusValue('L_WriteSpeed') . "\n";
//        echo '  L_OrderCount : ' . $this->getStatusValue('Tol_OrderCount') . "\n";
//        echo '  TS_UpCount : ' . $this->getStatusValue('TS_UpCount') . "\n";
//        echo '  Day_UpCount : ' . $this->getStatusValue('Day_UpCount') . "\n";



        echo '  StartTime : ' . $this->getStatusValue('StartTime') . "\n";
        echo '  EndTime : ' . $this->getStatusValue('EndTime') . "\n";
        echo '  ServiceRunTime : ' . $this->getStatusValue('ServiceRunT') . "\n";
        echo '  WebsiteFalseCount : ' . $this->getStatusValue('WebFalse') . "\n";
        echo '  WebsiteOverTimeCount : ' . $this->getStatusValue('WebOverT') . "\n";
        echo '  LPageFalseCount : ' . $this->getStatusValue('LPageFalse') . "\n";
        echo '  LPageOverTimeCount : ' . $this->getStatusValue('LPageOverT') . "\n";
        echo '  LinkFalseCount : ' . $this->getStatusValue('LinkFalse') . "\n";
        echo '  LinkOverTimeCount : ' . $this->getStatusValue('LinkOverT') . "\n";
        echo '  LinkDealAble : ' . $this->getStatusValue('DealAble') . "\n";

        echo '  TotalErrorLogCount : ' . $this->getStatusValue('T_Err_Count') . "\n";
        echo '  Service ExceptionCount : ' . $this->getStatusValue('S_Ex_Count') . "\n";
        echo '  TimingService ExceptionCount : ' . $this->getStatusValue('T_Ex_Count') . "\n";
    }

    /**
     * 查看日志
     *
     * @param int $logNum
     *            显示的日志数量
     * @param String $logFile
     *            写入日志文件
     * @return void
     */
    protected function cmdLogs($logNum, $logFile = '')
    {
        $logList = $this->getLog($logNum);
        $logFile = trim($logFile);
        $logContents = '';
        foreach ($logList as $log) {
            $logContents .= $log . "\n";
        }
        if ($logFile == '') {
            echo $logContents;
        } else {
            $result = file_put_contents($logFile, $logContents);
            if ($result === false) {
                echo "Log file failed.\n";
            } else
                echo "The log has been successfully written.\n";
        }
    }
}